package com.abc.cloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.abc.cloud.dto.menu.MenuDto;
import com.abc.cloud.dto.menu.MenuTree;
import com.abc.cloud.vo.menu.MenuSave;
import com.abc.cloud.vo.menu.MenuUpdate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.MenuDao;
import com.abc.cloud.entity.MenuEntity;
import com.abc.cloud.service.MenuService;


@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements MenuService {

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<MenuEntity> page = this.page(
                new Query<MenuEntity>().getPage(params),
                new QueryWrapper<MenuEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public List<MenuEntity> queryUserMenuList(Long userId) {
        return this.baseMapper.queryUserMenuList(userId);
    }

    @Override
    public List<MenuEntity> getMenuAllList() {
        return this.baseMapper.getMenuAllList();
    }

    @Override
    public List<MenuTree> getMenuList() {
        List<MenuEntity> adminMenuList = this.baseMapper.selectMenuList();
        List<MenuTree> adminMenuTrees = new ArrayList<>();
        for(MenuEntity adminMenu: adminMenuList){
            MenuTree parTree = new MenuTree();
            List<MenuEntity> adminMenuChildTree = new ArrayList<>();
            // 找出父树
            if(adminMenu.getParentId().equals(0L)){
                // 找子树
                for(MenuEntity adminMenuitem: adminMenuList){
                    if (adminMenuitem.getParentId().equals(adminMenu.getId())){
                        adminMenuChildTree.add(adminMenuitem);
                    }
                }
                parTree.setTitle(adminMenu.getTitle());
                parTree.setId(adminMenu.getId());
                parTree.setHidden(adminMenu.getHidden());
                parTree.setCreateTime(adminMenu.getCreateTime());
                parTree.setIcon(adminMenu.getIcon());
                parTree.setStatus(adminMenu.getStatus());
                parTree.setMenuTrees(adminMenuChildTree);
                adminMenuTrees.add(parTree);
            }
        }
        return adminMenuTrees;
    }

    @Override
    public MenuEntity updateMenu(MenuUpdate adminMenuUpdate) {
        QueryWrapper<MenuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", adminMenuUpdate.getId());
        queryWrapper.eq("status", 1);
        MenuEntity tbMenu = this.baseMapper.selectOne(queryWrapper);
        // 更新
        BeanUtils.copyProperties(adminMenuUpdate, tbMenu);
        int i = this.baseMapper.updateById(tbMenu);
        if (i <= 0){
            return null;
        }
        return this.baseMapper.selectById(adminMenuUpdate.getId());
    }

    @Override
    public MenuEntity saveMenu(MenuSave adminMenuSave) {
        MenuEntity tbMenu = BeanUtil.copyProperties(adminMenuSave, MenuEntity.class);
        int insert = this.baseMapper.insert(tbMenu);
        if(insert <= 0){
            return null;
        }
        return tbMenu;
    }

    @Override
    public List<MenuEntity> getParentMenu() {
        QueryWrapper<MenuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("parent_id", 0);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<MenuDto> getUserMenu(String username) {
        return this.baseMapper.selectMenuByUsername(username);
    }
}